<?php 
function dbconnect() 
{ 
	$db_host='localhost';
	$db_user='root';
	$db_pass='';
	$db_data='solio';

	static $connect = null; 
	if ($connect === null) { 
		$connect = mysql_connect($db_host,$db_user,$db_pass);
		mysql_select_db($db_data);		
	} 
	return $connect; 
} 


function khongdau($str) {
        $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
        $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
        $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
        $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
        $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
        $str = preg_replace("/(đ)/", 'd', $str);
        $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|A)/", 'a', $str);
        $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|E)/", 'e', $str);
        $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ|I)/", 'i', $str);
        $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|O)/", 'o', $str);
        $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|U)/", 'u', $str);
        $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ|Y)/", 'y', $str);
        $str = preg_replace("/(Đ|D)/", 'd', $str);
        $str = preg_replace("/ /", "-", $str);
        $str = str_replace(" ", "-", str_replace("&*#39;","",$str));
        $str = stripslashes($str);
        $str = str_replace(array(":",".","'",","),"", str_replace("&*#39;","",$str));
        $str = strtolower($str);
    return $str;
}

function mnu_sanpham() 
{ 
	$mnusp = mysql_query("SELECT * FROM categories ORDER BY sort",dbconnect()); 
	$result = array(); 
	while ($all = mysql_fetch_object($mnusp)) { 
		$result[] = $all; 
	} 
	return $result; 
} 


function get_listnews() 
{ 
	$listnews = mysql_query("SELECT * FROM news ORDER BY sort",dbconnect()); 
	$result = array(); 
	while ($all = mysql_fetch_object($listnews)) { 
		$result[] = $all; 
	} 
	return count($result); 
} 


function get_news($id) 
{ 
	$list = mysql_query("SELECT * FROM news WHERE id = $id ORDER BY sort",dbconnect()); 
	$result = array(); 
	while ($all = mysql_fetch_object($list)) { 
		$result[] = $all; 
	} 
	return $result;  
} 


function get_2news($page) 
{ 
	$q = mysql_query("SELECT * FROM news ORDER BY sort LIMIT 2 OFFSET 2*($page-1)",dbconnect()); 
        $result = array(); 
	while ($all = mysql_fetch_object($q)) { 
		$result[] = $all; 
	} 
	return $result; 
} 

function get_allproducts() 
{ 
	$list = mysql_query("SELECT * FROM products ORDER BY sort",dbconnect()); 
	$result = array(); 
	while ($all = mysql_fetch_object($list)) { 
		$result[] = $all; 
	} 
	return $result; 
} 


function get_products($id) 
{ 
	$list = mysql_query("SELECT * FROM products WHERE cat = $id ORDER BY sort",dbconnect()); 
	$result = array(); 
	while ($all = mysql_fetch_object($list)) { 
		$result[] = $all; 
	} 
	return $result; 
} 


function get_info($table) 
{ 
	$info = mysql_query("SELECT * FROM $table",dbconnect()); 
	return mysql_fetch_array($info); 
} 



///////////////////////////////////////////////////////////////////////////////////////////////////////////////
function get_news1($id) 
{ 
	$news_req = mysql_query("SELECT * FROM news WHERE id='$news_id'",dbconnect()); 
	return mysql_fetch_array($news_req); 
} 


function get_comment($news_id) 
{ 
	$comment_req = mysql_query("SELECT * FROM commentaires WHERE news_id='$news_id'",dbconnect()); 
	$result = array(); 
	while ($comment = mysql_fetch_array($comment_req)) { 
		$result[] = $comment; 
	} 
	return $result; 
} 


function insert_comment($comment) 
{ 
	mysql_query("INSERT INTO commentaires SET news_id='', 
                                          auteur='".mysql_real_escape_string($comment['auteur'])."', 
                                          texte='".mysql_real_escape_string($comment['texte'])."', 
                                          date=NOW()" ,dbconnect() ); 
}


function danhmuc()
{
	$danhmuc=mysql_query("SELECT * FROM danhmuc ORDER BY stt asc",dbconnect());
		$arr_dm = array();
		$arr_tl = array();
		$arr_result = array();
		while($rdm=mysql_fetch_object($danhmuc))
			{
				$arr_dm[] = $rdm; 
				$arr_tltemp = array();
				$theloai=mysql_query("SELECT * FROM loaithe WHERE iddm = ".$rdm->Id." ORDER BY stt asc",dbconnect());
						while($rtheloai=mysql_fetch_object($theloai))
							{ 
								$arr_tltemp[] = $rtheloai; 
							}				
				$arr_tl[] = $arr_tltemp;
				unset($arr_tltemp);
			}
			$arr_result[] = $arr_dm;
			$arr_result[] = $arr_tl;
	return $arr_result;
}

function get_cat_name($id) 
{ 
	$name = mysql_query("SELECT d.Name as DM, l.Name as TL from danhmuc d inner join loaithe l on d.Id = l.iddm where l.Id ='$id'",dbconnect()); 
	return mysql_fetch_array($name); 
} 

function get_cat_product($id) 
{ 
	$name = mysql_query("SELECT * from thecao where Loaithe ='$id'",dbconnect()); 
        $result = array(); 
	while ($all = mysql_fetch_object($name)) { 
		$result[] = $all; 
	} 
	return $result; 
} 


function get_help() 
{ 
	$q = mysql_query("SELECT * from helps where Status = 1 order by Id ASC",dbconnect()); 
        $result = array(); 
	while ($all = mysql_fetch_object($q)) { 
		$result[] = $all; 
	} 
	return $result; 
} 

function get_page() 
{ 
	$q = mysql_query("SELECT count(Id) as total from news where Status = 1",dbconnect()); 
	return mysql_fetch_array($q); 
} 

function get_huongdan() 
{ 
	$q = mysql_query("SELECT * from huongdan where Status = 1 order by Id ASC",dbconnect()); 
        $result = array(); 
	while ($all = mysql_fetch_object($q)) { 
		$result[] = $all; 
	} 
	return $result; 
} 

function get_cart($idlist) 
{ 
    if($idlist):
	$q = mysql_query("SELECT * from thecao WHERE Id IN (".$idlist.")",dbconnect()); 
        $result = array(); 
            while ($all = mysql_fetch_object($q)) { 
                    $result[] = $all; 
            } 
	return $result; 
     endif;
} 


function get_payment()
{ 
	$q = mysql_query("SELECT * from paygate where status=1 order by paygate_id",dbconnect()); 
        $result = array(); 
            while ($all = mysql_fetch_object($q)) { 
                    $result[] = $all; 
            } 
	return $result; 
} 


